
//
//  WheelView.m
//  HouseOfWillow
//
//  Created by ODSdev2 on 15/11/2.
//  Copyright (c) 2015年 liubenwang. All rights reserved.
//

#import "WheelView.h"
//滚轮的可见高度(包含标题栏)
#define wheelHeight 250
#define ToolBackgroundColor UIColorFromRGB(0X7c7c7c)

@interface WheelView()
<
    UIPickerViewDataSource,
    UIPickerViewDelegate
>
{
    UILabel * titleView;
    NSIndexPath * indexPath0;
    NSIndexPath * indexPath1;
    NSIndexPath * indexPath2;
}
/* 自带pickView */
@property (nonatomic,strong)  UIPickerView * pickView;
@property (nonatomic, assign) NSInteger columns;/**<滚轮的数目*/
@property (nonatomic, strong) NSArray * column0Array;/**<数组0*/
@property (nonatomic, strong) NSArray * column1Array;/**<数组1*/
@property (nonatomic, strong) NSArray * column2Array;/**<数组2*/
@property (nonatomic, strong) NSString * title;/**<标题*/
@property (nonatomic, strong) Oneblock returnBackOne;/**<触发点击方法,返回一个*/
@property (nonatomic, strong) Threeblock returnBackThree;/**<触发点击方法,返回三个*/
@end
@implementation WheelView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _columns = 1;
        _column0Array = [[NSArray alloc]init];
        _column1Array = [[NSArray alloc]init];
        _column2Array = [[NSArray alloc]init];
        indexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
        indexPath1 = [NSIndexPath indexPathForRow:0 inSection:1];
        indexPath2 = [NSIndexPath indexPathForRow:0 inSection:2];
    }
    return self;
}
- (void)loadView{
    
    UIView * topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height-wheelHeight)];
    topView.backgroundColor = [UIColor clearColor];
    [self addSubview:topView];
    UITapGestureRecognizer * topTouch = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeSelf)];
    [topView addGestureRecognizer:topTouch];
    
    UIView * botomView = [[UIView alloc]initWithFrame:CGRectMake(0, topView.bottom, self.width, wheelHeight)];
    botomView.backgroundColor= [UIColor whiteColor];
    [self addSubview:botomView];
    //标题栏
    titleView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.width, 50)];
    titleView.text = _title;
//    titleView.textColor = LOGBtnColor;
//    titleView.font = LOGBtnTitleFont(18);
    titleView.textAlignment = NSTextAlignmentCenter;
    titleView.layer.borderColor = [UIColor whiteColor].CGColor;
    titleView.layer.borderWidth = 1;
    titleView.backgroundColor = ToolBackgroundColor;
    [botomView addSubview:titleView];
    NSArray * titleArr = @[@"取消",@"确定"];
    for (int i = 0; i< 2; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0+i*(titleView.width-50), 1, 50, titleView.height-2);
        button.tag = 100+i;
        button.backgroundColor = ToolBackgroundColor;
//        button.titleLabel.font = LOGBtnTitleFont(15);
        [button setTitle:titleArr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [botomView addSubview:button];
    }
    _pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, titleView.bottom+(wheelHeight-40-180)/2, self.width, 180)];
//    _pickView.center = CGPointMake(botomView.width/2, botomView.center.y+20);
    _pickView.backgroundColor = [UIColor whiteColor];
    _pickView.delegate = self;
    _pickView.dataSource = self;
    _pickView.showsSelectionIndicator = YES;
    [botomView addSubview:_pickView];
}
//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
//    if (_columns==1) {
//        return self.width;
//    }
//    return self.width/_columns;
//}
//- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
//    return pickerView.height;
//}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return _columns;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component==0) {
        return _column0Array.count;
    }else if (component==1){
        id element = _column1Array.firstObject;
        if ([element isKindOfClass:[NSArray class]]) {
            
            return [_column1Array[indexPath0.row] count];
        }
        return _column1Array.count;
    }
    id first = _column2Array.firstObject;
    if ([first isKindOfClass:[NSArray class]]) {
        NSArray * element = _column2Array[indexPath0.row];
        id subElement = element[indexPath1.row];
        if ([subElement isKindOfClass:[NSArray class]]) {
            return [subElement count];
        }
    }
    return _column2Array.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if (component==0) {
        id element = _column0Array[row];
        if ([element isKindOfClass:[NSString class]]) {
            return (NSString *)element;
        }else{
            return @"NOSTRING";
        }
    }else if (component==1){
        id element = _column1Array[row];
        if ([element isKindOfClass:[NSString class]]) {
            return (NSString *)element;
        }else{
            if (indexPath0.row>_column1Array.count-1) {
                return @"TOOMORE";
            }
            if (![element isKindOfClass:[NSArray class]]) {
                return @"NOARRAY";
            }
            //取出第二列
            NSArray * midArray = _column1Array[indexPath0.row];
            //取出第二列的元素
            id midArrayElement = midArray[row];
            if (![midArrayElement isKindOfClass:[NSString class]]) {
                return @"NOSTRING";
            }
            return (NSString *)midArrayElement;
        }
    }else{
        id element = _column2Array[row];
        if ([element isKindOfClass:[NSString class]]) {
            return (NSString *)element;
        }else{
            if (indexPath0.row>_column2Array.count-1) {
                return @"TOMORE";
            }
            //取出第一层
            NSArray * rightArray = _column2Array[indexPath0.row];
            if (indexPath1.row>rightArray.count) {
                return @"TOMORE";
            }
            //取出第二层
            id rightElement = rightArray[indexPath1.row];
            if (![rightElement isKindOfClass:[NSArray class]]){
                return @"NOARRAY";
            }
            NSArray * rArray = (NSArray *)rightElement;
            //取出第三列的元素
            id rightArrayElement = rArray[row];
            if (![rightArrayElement isKindOfClass:[NSString class]]) {
                return @"NOSTRING";
            }
            return (NSString *)rightArrayElement;
        }
    }
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (component==0) {
        indexPath0 = [NSIndexPath indexPathForRow:row inSection:component];
        id element = _column1Array.firstObject;
        if ([element isKindOfClass:[NSString class]]) {
            return;
        }
        if (_columns>1) {
            //如果是两列刷新第二列
            [pickerView reloadComponent:1];
        }
        if (_columns>2) {
            //如果是三列刷新第三列
            NSArray * midArr = _column2Array[indexPath0.row];
            if (indexPath1.row>midArr.count-1) {
                indexPath1 = [NSIndexPath indexPathForRow:midArr.count-1 inSection:1];
            }
            
            NSArray * rightArr = _column2Array[indexPath0.row][indexPath1.row];
            if (indexPath2.row>rightArr.count-1) {
                indexPath2 = [NSIndexPath indexPathForRow:rightArr.count-1 inSection:1];
            }
            [pickerView reloadComponent:2];
        }
    }else if (component==1){
        indexPath1 = [NSIndexPath indexPathForRow:row inSection:component];
        id element = _column1Array.firstObject;
        if ([element isKindOfClass:[NSString class]]) {
            return;
        }
        if (_columns>2) {
            //如果是三列刷新第三列
            [pickerView reloadComponent:2];
        }
    }else{
        indexPath2 = [NSIndexPath indexPathForRow:row inSection:component];
    }
}
- (void)btnClick:(UIButton *)btn{

    if (btn.tag==100) {
        [self removeSelf];
    }else{
        if (_columns==1) {
            if (_returnBackOne) {
                _returnBackOne(indexPath0.row);
            }
        }else{
            if (_returnBackThree) {
                if ([_column1Array[0] isKindOfClass:[NSString class]]) {
                    _returnBackThree(_column0Array[indexPath0.row],_column1Array[indexPath1.row],_column2Array[indexPath2.row]);
                }
                else
                {
                _returnBackThree(_column0Array[indexPath0.row],_column1Array[indexPath0.row][indexPath1.row],_column2Array[indexPath0.row][indexPath1.row][indexPath2.row]);
                }
            }
        }
        [self removeSelf];
    }
}
- (void)removeSelf{
    [UIView animateWithDuration:0.2 animations:^{
        self.top = SCREEN_HEIGHT;
    }];
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.2];
}
+ (void)wheelViewOneComponentWithTitle:(NSString *)title dataArray:(NSArray *)dataArray andSelectRow:(NSInteger)selectedRow Block:(Oneblock)block{

    WheelView * view = [[WheelView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    view.backgroundColor = [UIColor clearColor];
    view.columns = 1;
    view.title = title;
    view.column0Array = dataArray;
    view.returnBackOne = block;
    [view loadView];
    if (dataArray.count>selectedRow) {
        [view.pickView selectRow:selectedRow inComponent:0 animated:YES];
    }
    [RootWindow addSubview:view];
    [UIView animateWithDuration:0.2 animations:^{
        view.top = 0;
    }];
}
+ (void)wheelViewThreeComponentWithTitle:(NSString *)title leftArray:(NSArray *)leftArray midArray:(NSArray *)midArray rightArray:(NSArray *)rightArray andSelectState:(NSArray *)states Block:(Threeblock)block{
    
    WheelView * view = [[WheelView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    view.backgroundColor = [UIColor clearColor];
    view.columns = 3;
    view.title = title;
    view.column0Array = leftArray;
    view.column1Array = midArray;
    view.column2Array = rightArray;
    view.returnBackThree = block;
    [view loadView];
    if (states) {
        for (int i=0; i<states.count; i++) {
            NSNumber * row = states[i];
            if (i==0) {
                if (leftArray.count>[row integerValue]) {
                    [view.pickView selectRow:[row integerValue] inComponent:i animated:YES];
                }
            }else if (i==1) {
                if (midArray.count>[row integerValue]) {
                    [view.pickView selectRow:[row integerValue] inComponent:i animated:YES];
                }
            }else if (i==2) {
                if (rightArray.count>[row integerValue]) {
                    [view.pickView selectRow:[row integerValue] inComponent:i animated:YES];
                }
            }
        }
    }
    [RootWindow addSubview:view];
    [UIView animateWithDuration:0.2 animations:^{
        view.top = 0;
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
